package com.example.demo1.service;

import java.sql.Timestamp;
import java.util.List;

import com.example.demo1.exception.ExceptionEnum;
import com.example.demo1.exception.ToprsException;
import com.example.demo1.maper.CategoryMapper;
import com.example.demo1.model.Category;
import com.example.demo1.requestModel.categoriesReq;
import com.fasterxml.jackson.databind.util.BeanUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    public List<Category> getAllCategory() {
        List<Category> list = categoryMapper.queryAll();
        return list;
    }

    public void addCategory(categoriesReq categoriesReq) {
        Category c = new Category();
        c.setCreateTime(new Timestamp(System.currentTimeMillis()));
        BeanUtils.copyProperties(categoriesReq, c);

        Category cOld = categoryMapper.queryByName(c.getName());
        if (cOld != null) {
            throw new ToprsException(ExceptionEnum.ITEM_EXTI);
        }

        categoryMapper.inset(c);
    }

}
